<?php

class system extends Module {

	public function __construct () {
		parent::__construct ('system');
		
		$this->extend ('__custom_system');
	}
	
	public function __default () {
		throw new NotFound;
	}
	
	/**
	 * Не удалять, для cmsmagazine
	 */
	public function cms () {
		$this->flush('explaycms ' . Regedit::getInstance()->get ('//cms/version'), 'text/plain');
	}
	
	/**
	 * Получить объект
	 * @param mixed $type тип (название или id)
	 * @param integer $id id объекта
	 * @param string $template шаблон
	 */
	public function getObject ($type, $id, $template = '') {
		if (is_numberic ($type)) {
			$oType = TypesController::getInstance()->getTypeById ($type);
		} else {
			$oType = TypesController::getInstance()->getTypeByName ($type);
		}
		
		$response = new ModuleResponse;
		$response->setTemplate ($template);
		
		if (!$oType) {
			return $response;
		}
		
		if ($object = ObjectsController::getInstance()->getObject ($oType, $id)) {
			$response->addObject ($object);
		}
		
		return $response;
	}
	
	public function getVoted ($rating, $allowVote, $objectTypeId, $objectId, $formatRating = false) {
		$oVoteType = TypesController::getInstance()->getTypeByName ('vote');
		
		$sel = new Selection;
		$sel->addType ($oVoteType);
		$sel->addFilter ('user_id', ExplaySession::getUserId());
		$sel->addFilter ('object_id', $objectId);
		$sel->addFilter ('type_id', $objectTypeId);
		$sel->addSelectedProperty('mark');
		$sel->addLimit (1);
		
		$res = $sel->getList ();
		
		$response = new ModuleResponse;
		
		$result = array (
			'rating' => $formatRating ? number_format ($rating, 2, ',', ' ') : $rating,
			'allow' => $allowVote,
			'type_id' => $objectTypeId,
			'object_id' => $objectId,
			'mark' => ''
		);
		
		if (isset ($res[0])) {
			$result['mark'] = $res[0]['mark'];
			$result['allow'] = '0';
		}
		
		$response->addObject (new Array2XML ('vote', $result));
		
		return $response;
	}

}